package android.moodle;

import android.Graphics.GameRenderer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.javaClasses.Client;
import android.javaClasses.Content;
import android.javaClasses.DataHelper;
import android.javaClasses.Game;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class ClientActivity extends Activity

{
    private EditText et;
    private Client c;
    private TextView tv;
    private Context context;
    private GameRenderer renderer;
    private GLSurfaceView glView;
    private TextView gameHeader;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamescreen);
        Bundle extras = getIntent().getExtras();
        String player = extras.getString("playername");
        String ip = extras.getString("serverAddress");


        final Vibrator v = (Vibrator) getSystemService( Context.VIBRATOR_SERVICE );

        // Get the width and height of the screen, so it's scalable
        Display display = getWindowManager().getDefaultDisplay();
        float width = display.getWidth();

        // Subtracts 25 pixels because that is the height of the statusbar
        float height = display.getHeight() - 25;

        //Declares the GLSurfaceView that is used for drawing the game
        glView = (GLSurfaceView) findViewById(R.id.glsurfaceview);

        //Declares the Gamerenderer that is used to draw the game
        renderer = new GameRenderer( this, width, height, 3, 2);
        glView.setRenderer( renderer );

        gameHeader = (TextView) findViewById(R.id.gameheader);

        glView.setOnTouchListener( new View.OnTouchListener()
        {
            public boolean onTouch( View view, MotionEvent motionEvent )
            {
                v.vibrate(100);
                float xValue = motionEvent.getX();
                float yValue = motionEvent.getY();
                renderer.drawSymbol(xValue, yValue);
                sendToServer(new Content(Content.SEND_CHOSEN_CELL, xValue, yValue));

                gameHeader.setText( renderer.getHeader() );
                return false;
            }
        });

        context = this;
        c = new Client(ip, mHandler);
        c.startReadFromStream();
        c.writeToStream(new Content(Content.SEND_USERNAME, player));

    }

    private void sendToServer(Content content) {
        c.writeToStream(content);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Content.GAME_FINISHED:
                    ArrayList<String> players = msg.getData().getStringArrayList("players");
                    String winner = msg.getData().getString("winner");
                    Game theGame = new Game(players, winner);
                    DataHelper helper = new DataHelper(context);
                    helper.insert(theGame);
                    startActivity(new Intent(context, ScoresActivity.class));
                    break;
                case Content.SEND_CHOSEN_CELL:
                    float xValue = msg.getData().getFloat("xValue");
                    float yValue = msg.getData().getFloat("yValue");
                    renderer.drawSymbol(xValue, yValue);
                    break;
                case Content.START_STATUS:
                    int typeOfGame =  msg.getData().getInt("typeOfGame");
                    int numPlayers = msg.getData().getInt("numPlayers");

            }
        }
    };


}
